# Copyright 2022 Ant Group Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("//bazel:psi.bzl", "psi_cc_binary", "psi_cc_library", "psi_cc_test")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_cc//cc:defs.bzl", "cc_proto_library")

package(default_visibility = ["//visibility:public"])

proto_library(
    name = "psi_proto",
    srcs = ["psi.proto"],
)

cc_proto_library(
    name = "psi_cc_proto",
    deps = [":psi_proto"],
)

psi_cc_library(
    name = "memory_psi",
    srcs = ["memory_psi.cc"],
    hdrs = [
        "memory_psi.h",
    ],
    deps = [
        ":prelude",
        ":psi_cc_proto",
        "//psi/psi/core:ecdh_psi",
        "//psi/psi/operator",
        "//psi/psi/operator:factory",
        "//psi/psi/utils",
    ],
)

psi_cc_library(
    name = "prelude",
    hdrs = [
        "prelude.h",
    ],
    deps = [
        ":psi_cc_proto",
        "//psi/proto:psi_v2_cc_proto",
    ],
)

psi_cc_test(
    name = "memory_psi_test",
    srcs = ["memory_psi_test.cc"],
    deps = [
        ":memory_psi",
        "//psi/psi/utils:test_utils",
    ],
)

psi_cc_library(
    name = "bucket_ub_psi",
    srcs = ["bucket_ub_psi.cc"],
    hdrs = [
        "bucket_ub_psi.h",
    ],
    deps = [
        ":prelude",
        ":psi_cc_proto",
        "//psi/psi/core:ecdh_oprf_psi",
        "//psi/psi/utils:batch_provider",
        "//psi/psi/utils:csv_checker",
        "//psi/psi/utils:csv_header_analyzer",
        "//psi/psi/utils:ec_point_store",
        "//psi/psi/utils:progress",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "@yacl//yacl/utils:scope_guard",
    ],
)

psi_cc_library(
    name = "bucket_psi",
    srcs = ["bucket_psi.cc"],
    hdrs = [
        "bucket_psi.h",
    ],
    deps = [
        ":bucket_ub_psi",
        ":memory_psi",
        ":prelude",
        ":psi_cc_proto",
        "//psi/psi/utils:batch_provider",
        "//psi/psi/utils:csv_checker",
        "//psi/psi/utils:csv_header_analyzer",
        "//psi/psi/utils:ec_point_store",
        "//psi/psi/utils:file",
        "@boost//:uuid",
    ],
)

psi_cc_test(
    name = "bucket_psi_test",
    srcs = ["bucket_psi_test.cc"],
    deps = [
        ":bucket_psi",
        "@yacl//yacl/utils:scope_guard",
    ],
)

psi_cc_test(
    name = "bucket_ub_psi_test",
    srcs = ["bucket_ub_psi_test.cc"],
    deps = [
        ":bucket_psi",
        "//psi/psi/utils:test_utils",
    ],
)

psi_cc_library(
    name = "interface",
    srcs = ["interface.cc"],
    hdrs = ["interface.h"],
    deps = [
        ":recovery",
        ":trace_categories",
        "//psi/proto:psi_v2_cc_proto",
        "//psi/psi:bucket_psi",
        "//psi/psi/utils:advanced_join",
        "//psi/psi/utils:index_store",
        "@boost//:uuid",
        "@com_github_google_perfetto//:perfetto",
        "@com_google_absl//absl/status",
        "@yacl//yacl/link",
    ],
)

psi_cc_library(
    name = "factory",
    srcs = ["factory.cc"],
    hdrs = ["factory.h"],
    deps = [
        "//psi/psi/ecdh:receiver",
        "//psi/psi/ecdh:sender",
        "//psi/psi/kkrt:receiver",
        "//psi/psi/kkrt:sender",
        "//psi/psi/rr22:receiver",
        "//psi/psi/rr22:sender",
        "@yacl//yacl/base:exception",
    ],
)

psi_cc_binary(
    name = "main",
    srcs = ["main.cc"],
    deps = [
        ":factory",
        ":kuscia_adapter",
        ":trace_categories",
        "//psi:version",
        "@boost//:algorithm",
        "@boost//:uuid",
        "@com_github_gflags_gflags//:gflags",
    ],
)

psi_cc_library(
    name = "trace_categories",
    srcs = ["trace_categories.cc"],
    hdrs = ["trace_categories.h"],
    deps = [
        "@com_github_google_perfetto//:perfetto",
    ],
)

psi_cc_library(
    name = "recovery",
    srcs = [
        "recovery.cc",
    ],
    hdrs = [
        "recovery.h",
    ],
    deps = [
        "//psi/proto:psi_v2_cc_proto",
        "//psi/psi/cryptor:ecc_cryptor",
        "//psi/psi/io",
        "@yacl//yacl/base:exception",
        "@yacl//yacl/link",
    ],
)

psi_cc_test(
    name = "recovery_test",
    srcs = ["recovery_test.cc"],
    deps = [
        ":recovery",
        "//psi/psi/cryptor:cryptor_selector",
    ],
)

psi_cc_library(
    name = "kuscia_adapter",
    srcs = [
        "kuscia_adapter.cc",
    ],
    hdrs = [
        "kuscia_adapter.h",
    ],
    deps = [
        "//psi/proto:kuscia_cc_proto",
        "//psi/proto:psi_v2_cc_proto",
        "@com_github_tencent_rapidjson//:rapidjson",
        "@yacl//yacl/base:exception",
    ],
)

psi_cc_test(
    name = "kuscia_adapter_test",
    srcs = ["kuscia_adapter_test.cc"],
    deps = [
        ":kuscia_adapter",
    ],
)

psi_cc_test(
    name = "psi_test",
    srcs = ["psi_test.cc"],
    deps = [
        ":factory",
        "//psi/psi/utils:arrow_csv_batch_provider",
        "@boost//:uuid",
        "@yacl//yacl/utils:scope_guard",
    ],
)

psi_cc_library(
    name = "bucket",
    srcs = ["bucket.cc"],
    hdrs = ["bucket.h"],
    deps = [
        "//psi/proto:psi_v2_cc_proto",
        "//psi/psi:prelude",
        "//psi/psi:recovery",
        "//psi/psi/utils",
        "//psi/psi/utils:hash_bucket_cache",
    ],
)
